#ifndef __WeakReference_hpp__
#define __WeakReference_hpp__

#include "ReferenceCounted.hpp"

namespace ge {

//----------------------------------------------------------------------------//
// WeakReference
//----------------------------------------------------------------------------//

class WeakReference : public virtual ReferenceCounted
{
 friend class ReferenceCounted;

 public:

     ReferenceCounted* Object( void ) const
     {
          return const_cast< ReferenceCounted* >( m_object );
     }

     template < class T >
     T* Object( void ) const
     {
          return dynamic_cast< T* >( Object() );
     }

     virtual WeakReference* GetWeakRef( void ) const
     {
          return const_cast< WeakReference* >( this );
     }

 protected:

     WeakReference( ReferenceCounted* _object ) : m_object( _object )
     {
          m_refcnt = 1;
     }

     virtual ~WeakReference( void )
     {
     }

     static void _Reset( WeakReference* _weakref )
     {
          _weakref -> m_object = 0;
          if( !( -- _weakref -> m_refcnt ) ) delete _weakref;
     }

     ReferenceCounted* m_object;

 private:

     WeakReference( void );
};

} // namespace ge

#endif//__WeakReference_hpp__